<template>
	<view class="u-page">
		<!-- 基本信息 -->
		<view class="u-demo-block" >

			<view class="u-demo-block__content">
				<u--form
					labelPosition="left"
					labelWidth="200rpx"
					:model="userInfo"
					ref="form1"
				>
	<!-- 			<u-form-item label="晋升职位" borderBottom  @click="showPop1" required="true">
					<u--input v-model="role_text" placeholder="请选择申请晋升职位" border="none" class="role_text" style="font-size: 0.8rem;"></u--input>
					<lzcPicker ref="lzcPicker1" @change="bindPickerChange3" :pickerList="roleList" pickerTittle='选择晋升职位' />
				</u-form-item>	 -->
						<u-form-item
									label="晋升职位"
									prop="userInfo.role_text"
									borderBottom
									@click="showSex = true;"
							>
								<u--input
										v-model="role_text"
										disabled
										disabledColor="#ffffff"
										placeholder="请选择晋升职位"
										border="none"
								></u--input>
								<u-icon
										slot="right"
										name="arrow-right"
								></u-icon>
							</u-form-item>
					
					<u-form-item label="竞业保密协议" required="true" borderBottom >
						<!-- 上传图片 -->
						<u-upload
								:fileList="data.secret_agreement"
								:deletable="userInfo.can_edit_image"
								@afterRead="afterRead"
								@delete="deletePic"
								:sizeType="sizeType"
								name="secret_agreement"
								multiple
								maxCount="4"
								width="150rpx"
								height="210rpx"
							>
							<image src="/static/image/icon/photo.png" mode="widthFix" style="width: 150rpx;"></image>
							</u-upload>
					</u-form-item>
					
					<u-form-item label="承诺书" required="true" borderBottom >
						<!-- 上传图片 -->
						<u-upload
								:fileList="data.promise_book"
								:deletable="userInfo.can_edit_image"
								:sizeType="sizeType"
								@afterRead="afterRead"
								@delete="deletePic"
								name="promise_book"
								multiple
								maxCount="1"
								width="150rpx"
								height="210rpx"
							>
							<image src="/static/image/icon/photo.png" mode="widthFix" style="width: 150rpx;"></image>
							</u-upload>
							
					</u-form-item>
						
			
					
					<u-form-item label="晋升培训结业证书" borderBottom required="true">
						<!-- 上传图片 -->
						<u-upload
								:fileList="data.winding_up_book"
								:deletable="userInfo.can_edit_image"
								:sizeType="sizeType"
								@afterRead="afterRead"
								@delete="deletePic"
								name="winding_up_book"
								multiple
								maxCount="1"
								width="150rpx"
								height="210rpx"
							>
							<image src="/static/image/icon/photo.png" mode="widthFix" style="width: 150rpx;"></image>
							</u-upload>
							
					</u-form-item>
					
				</u--form>
			</view>
			<u-button v-if="userInfo.can_edit"  type="primary" text="提交" customStyle="margin-top: 50px" @click="submit"></u-button>			
		</view>
				<u-action-sheet
						:show="showSex"
						:actions="roleList"
						title="请选择晋升职位"
						@close="showSex = false"
						@select="sexSelect"
				>
				</u-action-sheet>
		<yfx-tooltip ref="tooltip"></yfx-tooltip>
		<zero-loading v-if="loading == true" type="gear" mask="true" maskOpacity="0.3"></zero-loading>
	</view>
</template>

<script>
	import lzcPicker from '@/components/lzc-picker/lzc-picker.vue'
	export default {
		components: {
			lzcPicker
		},
		data() {
			return {
				showSex  : false,
				loading  : false,
				proveShow:false,
				upBaseUrl:this.$http.baseUrl,
				sizeType:['original'],
				userInfo:{
					can_edit : 1,
					can_edit_image : 1,
					
				},
				data:{
					id               : 0,
					apply_role       : 0,
					secret_agreement : [],
					promise_book     : [],
					winding_up_book  : [],
				},
				status   :'',
				role_text:'',
				roleList:[]
				
			}
		},
		onLoad(option) {
			if(option.id){
				this.data.id = option.id;
				this.getDetail(option.id);
			}
			this.getUserInfo();	
			this.getBankList();
		},
		methods: {
			sexSelect(e) {
				this.role_text = e.name;
				this.data.apply_role = e.id;
			},
			getDetail(id){
				if(id > 0){
					this.$http.getUpgradeDetail({id:id}).then(res => {
						if (res.code == 1) {
							this.data.apply_role = res.data.apply_role;
							this.data.secret_agreement = res.data.secret_agreement;
							this.data.promise_book = res.data.promise_book;
							this.data.winding_up_book = res.data.winding_up_book;
							this.role_text      = res.data.apply_role_text;
							this.status         = res.data.status;
							this.userInfo.can_edit = res.data.can_edit;
							this.userInfo.can_edit_image = res.data.can_edit;
							
						}
					});
				}
			},
			getBankList(){
				this.$http.getUpgradeRoleList({}).then(res => {
					if (res.code == 1 && res.data) {
						this.roleList = res.data.role_list;
					}
				});
			},
			submit() {
					let param = this.data;
					this.$http.saveUpgradeInfo(param).then(res => {
						if (res.code == 1) {
							uni.showToast({
								title: res.msg,
								duration: 2000,
								complete: function () {
								       // 2秒后关闭 toast 并返回上一页
								       setTimeout(function () {
								       	let pages = getCurrentPages();
								       	let prevPage = pages[pages.length - 2];//上一个页面
								       	//直接调用上一个页面的setData()方法，把数据存到上一个页面中去
								       	prevPage.setData({
								       		 isRefresh: 1 
								       	})
								          	uni.navigateBack(1);
								       }, 2000);
								    }
							});
						
						}else{
							if(res.msg == '非本人银行卡请上传代收凭证'){
								this.proveShow = true;
							}
							this.$refs.tooltip.open({
								msg: '错误',//标题
								content: res.msg,//提示内容
								showCancel: false//不显示取消按钮
							})
						}
					});
					
				},
				getUserInfo() {
					let param = {};
					this.$http.getUser(param).then(res => {
						if (res.code == 1) {
							
						}else{
							if(res.code == 401){
								var code = '';
								uni.login({
									provider: 'weixin',
									success: function(logRes) {
										code = logRes.code;
									}
								});
								// 获取code
								
								uni.getUserProfile({
									desc: '作为展示',
									success: res => {
										let param = {
											code: code,
											iv: res.iv,
											encryptedData: res.encryptedData,
											rawData: res.rawData,
											signature: res.signature,
											nickname: res.userInfo.nickName,
											avatar: res.userInfo.avatarUrl,
										};
										that.$http.authorization(param).then(res => {
											uni.hideToast();
											if (res.code == 1) {
												uni.setStorageSync('token', res.data.token);
												uni.setStorageSync('mobile',res.data.mobile?res.data.mobile:'');
												
												this.getUserInfo();
												
											}
										});
									},
									fail: err => {
										console.log('获取失败', err);
									}
								});
								
							}else{
								this.$refs.tooltip.open({
									msg: '提示',//标题
									content: res.msg,//提示内容
									showCancel: false,//不显示取消按钮
									confirm: () => {
										uni.navigateBack({
											delta: 1
										});
									} 
								})
							}
						}
						
						
					})
				},
				
			// 删除图片
				deletePic(event) {
					this.data[`${event.name}`].splice(event.index, 1)
				},
			      // 新增图片
			      async afterRead(event) {
			        // 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
			        let lists = [].concat(event.file)
			        let fileListLen = this.data[`${event.name}`].length
			        lists.map((item) => {
			          this.data[`${event.name}`].push({
			            ...item,
			            status: 'uploading',
			            message: '上传中'
			          })
			        })
			        for (let i = 0; i < lists.length; i++) {
			          const result = await this.uploadFilePromise(lists[i].url)
			          
			          let item = this.data[`${event.name}`][fileListLen]
			          this.data[`${event.name}`].splice(fileListLen, 1, Object.assign(item, {
			            status: 'success',
			            message: '',
			            url    :result
			          }))
			          fileListLen++;
			        }

			      },
				uploadFilePromise(url) {
					this.loading = true;
					return new Promise((resolve, reject) => {
						let a = uni.uploadFile({
							url: this.upBaseUrl + 'common/uploadImg',
							filePath: url,
							name: 'file',
							header: {
								'TOKEN': uni.getStorageSync('token')
							},
							formData: {},
							success: (res) => {
								setTimeout(() => {
									this.loading = false;
									resolve(JSON.parse(res.data).data.full_url)
								}, 1000)
							}
						});
					})
				},
		}
	}
</script>

<style>
.u-page{
		background: #fff;
		padding:0rpx 20rpx 50rpx
	}
  input {
    font-size: 0.8rem!important; /* 设置你想要的字体大小 */
  }
</style>
